<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserNotification;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    /**
     * 获取用户通知列表
     */
    public function index(User $user)
    {
        // 确保只能查看自己的通知
        if ($user->id !== auth()->id()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        $notifications = UserNotification::where('user_id', $user->id)
            ->orderBy('created_at', 'desc')
            ->paginate(20);

        return response()->json($notifications);
    }

    /**
     * 标记通知为已读
     */
    public function markAsRead(UserNotification $notification)
    {
        // 确保只能标记自己的通知
        if ($notification->user_id !== auth()->id()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        $notification->update([
            'is_read' => true,
            'read_at' => now()
        ]);

        return response()->json([
            'message' => 'Notification marked as read'
        ]);
    }
}